﻿using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace 自定义泛型仓储实现.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public abstract class BaseGenericityApiController<T> : ControllerBase where T : class, new()
    {
        //创建数据库对象 (用法和EF Dappper一样通过new保证线程安全)
        private SqlSugarClient _db = new SqlSugarClient(new ConnectionConfig()
        {
            ConnectionString = "server=.;database=testdb;uid=sa;pwd=123456",
            DbType = DbType.SqlServer,
            IsAutoCloseConnection = true
        });

        protected BaseGenericityApiController()
        {
        }

        [HttpPost]
        public virtual async Task<int> AddAsync(T obj)
        {
            
            var result = await _db.Insertable(obj).ExecuteCommandAsync();
            return result;
        }

        [HttpPost]
        public virtual async Task<int> DeleteAsync(T obj)
        {
            var result = await _db.Deleteable(obj).ExecuteCommandAsync();
            return result;
        }

        [HttpPost]
        public virtual async Task<int> EditAsync(T obj)
        {
            var result = await _db.Updateable(obj).ExecuteCommandAsync();
            return result;
        }

        [HttpPost]
        public virtual async Task<int> QueryAsync()
        {
            await Task.CompletedTask;
            return default;
        }
    }
}